/**
 * 主动欢迎语
 * 添加好友成功后，发送欢迎语
 *
 * 和“增强欢迎语”的区别是，那个是在企微自己发送欢迎语后发送额外的欢迎语。
 * 这个是直接由 Quntool 完成欢迎语发送。
 */
import type { OptionalFields } from '@gnlab/utils'
import { getLogger, Service } from '@gnlab/utils'
import { makeHTTPSAvatar } from '@/lib/https'
import type { Services } from '@/services'
import { API } from '@/services/common/api'
import { type Word, formatIrregularWord } from '@/services/common/definition'
import { friendlyQywxName } from '@/services/qywxs'

const logger = getLogger('services/features/normalWelcome') // eslint-disable-line @typescript-eslint/no-unused-vars

export interface BaseWelcomeConfig {
  name: string // 欢迎语名称
  enable: boolean // 此条规则是否启用
  wxids: string[] // 应用此欢迎语的企业微信
  content: Word[] // 欢迎语内容
}

export interface WelcomeConfig extends BaseWelcomeConfig {
  id: number
}

export function getEmptyWelcomeConfig(): BaseWelcomeConfig {
  return {
    name: '',
    enable: false,
    wxids: [],
    content: [],
  }
}

interface RawWelcomeConfig {
  Id: number
  Name: string
  WxIds: string[]
  Content: Word[]
  Enable: boolean
}
function formatRawWelcomeConfig(raw: RawWelcomeConfig): WelcomeConfig {
  return {
    id: raw.Id,
    name: raw.Name,
    wxids: raw.WxIds,
    content: raw.Content.map(formatIrregularWord),
    enable: raw.Enable,
  }
}

// 欢迎语配置涉及的企业微信信息
export interface ConfigQywx {
  wxid: string
  username: string
  nickname: string
  corpname: string
  friendlyName: string
  friendlyFullName: string
  avatar: string
  accountName: string
  accountRemark: string
  configId?: number | null // number: 此企业微信被加入了哪个配置，为 null 代表还没有被加入配置
}

interface RawConfigQywx {
  NickName: string
  UserName: string
  CorpName: string
  Avatar: string
  UserAccountName: string
  UserAccountRemarkName: string | null
  WxId: string
  ConfigId: number
}

function formatConfigQywx(raw: RawConfigQywx): ConfigQywx {
  const base = {
    wxid: raw.WxId,
    username: raw.UserName,
    nickname: raw.NickName,
    corpname: raw.CorpName,
    avatar: makeHTTPSAvatar(raw.Avatar),
    accountName: raw.UserAccountName,
    accountRemark: raw.UserAccountRemarkName ?? '',
    configId: raw.ConfigId === 0 ? null : raw.ConfigId,
  }
  return {
    ...base,
    friendlyName: friendlyQywxName(base, false),
    friendlyFullName: friendlyQywxName(base),
  }
}

export interface MessageVariable {
  id: number
  name: string
  regex: string
}
interface RawMessageVariable {
  Id: number
  Name: string
  Regex: string
}
function formatRawMessageVariable(raw: RawMessageVariable): MessageVariable {
  return {
    id: raw.Id,
    name: raw.Name,
    regex: raw.Regex,
  }
}

export class NormalWelcome extends Service<Services> {
  async loadConfigs() {
    return API('/NormalWelcome/GetConfigs', {
      formatter(data: RawWelcomeConfig[] | null) {
        return (data ?? []).map(formatRawWelcomeConfig)
      },
    })
  }

  async getQywxListForConfig(configId?: number) {
    return API('/NormalWelcome/WxUserList', {
      data: { ConfigId: configId },
      formatter(raw: RawConfigQywx[]) {
        return raw.map(formatConfigQywx)
      },
    })
  }

  async addOrUpdateConfig(config: BaseWelcomeConfig & { id?: number }) {
    return API('/NormalWelcome/SaveConfig', { data: config })
  }

  async removeConfig(id: number) {
    return API('/NormalWelcome/RemoveConfig', { data: { id } })
  }

  async loadVariables() {
    return API('/NormalWelcome/GetParams', {
      formatter(data: RawMessageVariable[] | null) {
        return (data ?? []).map(formatRawMessageVariable)
      },
    })
  }

  async addOrUpdateVariable(variable: OptionalFields<MessageVariable, 'id'>) {
    return API('/NormalWelcome/SaveParam', { data: variable })
  }

  async removeVariable(id: number) {
    return API('/NormalWelcome/RemoveParam', { data: { id } })
  }
}
